/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sda.levelup.library;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
 *
 * @author Дима
 */
public class Library
{
    
    LinkedList<String> listOfBooks;

    public Library() {
        listOfBooks=(LinkedList)FileMaster.getFileContent("D:\\Courses\\Library.txt");
    }
    
    
    public void addBookToFile(Book book){
        FileMaster.addToFile(book, "D:\\Courses\\Library.txt");
        listOfBooks.add(book.toString());
    }
    public void addBookToFile(int id, String name, String author){
        Book book = new Book(id, name, author);
        FileMaster.addToFile(book, "D:\\Courses\\Library.txt");
        listOfBooks.add(book.toString());
    }
    
    public Book findBookByName(String bookName){
        StringTokenizer tokenizer;
        String name;
        Book book = null;
        for (String temp : listOfBooks){
            tokenizer = new StringTokenizer(temp, "|");
            tokenizer.nextToken();
            tokenizer.nextToken();
            name=tokenizer.nextToken();
            if(bookName.equals(name)){
                book = new Book(temp);
            }
        }
        return book;
        
        /*
        HashMap<String, Book> map = new HashMap<String, Book>();
        for (String temp : listOfBooks){
            Book book = new Book(temp);
            map.put(book.getName(), book);
        }
        return map.get(bookName);*/
    }
    
    public Book findBookById(int id){
        StringTokenizer tokenizer;
        Book book = null;
        int booksId;
        for (String temp : listOfBooks){
            tokenizer = new StringTokenizer(temp, "|");
            booksId = Integer.parseInt(tokenizer.nextToken());            
            if(id==booksId){
                book = new Book(temp);
            }
        }
        return book;
        /*
        HashMap<Integer, Book> map = new HashMap<Integer, Book>();
        for (String temp : listOfBooks){
            Book book = new Book(temp);
            map.put(book.getId(), book);
        }
        return map.get(id);*/
    }    
    public List<Book> findAllAuthorsBooks(String author){
        
        List<Book> authorList = new LinkedList<Book>();
        for (String tmp : listOfBooks){
            Book book = new Book(tmp);
            if(book.getAuthor().equals(author)){
                authorList.add(book);
            }
        }
        return authorList;
    }
}
